//把配置项单独处理
// import store from '/store/index.js'; //vuex  
// let server_url='http://192.168.0.157:8012';//开发请求地址
// let server_url='http://127.0.0.1:8012';//开发请求地址
// let server_url='http://121.5.189.38:8012';//生产环境请求地址
let token = '';  //凭证
let server_url = process.env.NODE_ENV === 'development' ? 'http://bwapi.liuchaoai.com' : 'http://bwapi.liuchaoai.com' ; //环境配置
function service(options = {}) {
       // store.state.token && (token = store.state.token); //从vuex中获取登录凭证
       options.url = `${server_url}${options.url}`;
          //配置请求头
        options.header = {
        'content-type': 'application/x-www-form-urlencoded',
        // 'accessToken': `${token}` //Bearer 
    };
	if(options.method == undefined){
		options.method = 'GET'
	}
	// console.log('options',options)
    return new Promise((resolved, rejected) => {
                //成功
        options.success = (res) => {
              
            if (Number(res.data.code) == 0) {  //请求成功
                resolved(res.data);
            } else {
                uni.showToast({
                    icon: 'none',   
                    duration: 3000,
                    title: `${res.data.msg}`
                });
                rejected(res.data.msg);//错误
            }
        }
              //错误
        options.fail = (err) => {
            rejected(err); //错误
        }
        uni.request(options);
    });
}
//封装uploadFile上传文件请求
function sendUploadFile (url, filePath, name, data) {
	// var token = uni.getStorageSync('userInfo').token.token || '';
	// console.log(token,'正在请求uploadFile上传文件');
	return new Promise(function(resolve, reject) {
		uni.uploadFile({
			url: server_url + url, //请求接口地址
			filePath: filePath, //文件地址
			name: name,
			formData: data || '' ,
			// header: {
			// 	'token': token
			// },
			success(res){
				// console.log("请求成功的结果", res.statusCode,res);
				resolve(JSON.parse(res.data));
			},
			fail(err){
				// console.log("请求失败的结果", res);
				reject(res);
			}
		})
	})
}
// export default ;
export default{
	service,
	sendUploadFile
}